package com.dfrz.service.impl;

import com.dfrz.entity.Comment;
import com.dfrz.mapper.ICommentMapper;
import com.dfrz.service.CommentService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 29877
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    ICommentMapper iCommentMapper;
    @Override
    //将对应视频的所有评论信息按照类型分别存储
    public Map<String,Object> findAllCommentbyVideoId(Integer vid) {
        Map commentsort=new HashMap();
        List<Comment> allcomment=iCommentMapper.findAllCommentbyVideoId(vid);
        List<Comment> maincomment=new LinkedList<>();
        List<Comment> secondcomment=new LinkedList<>();
        Iterator<Comment> iterator=allcomment.iterator();
        while (iterator.hasNext()){
            Comment c1=iterator.next();
            if(c1.getCommentType()==1){
                maincomment.add(c1);
            }else {
                secondcomment.add(c1);
            }
        }
        commentsort.put("main",maincomment);
        commentsort.put("second",secondcomment);
        return commentsort;
    }

    @Override
    public Integer addComment(Comment comment) {

        return iCommentMapper.addComment(comment);
    }
}
